package com.an.framework;

import java.io.IOException;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import com.alibaba.druid.pool.DruidDataSource;

/**
 * ClassName: DataSourceConfig <br/>
 * Function: 阿里数据源配置类. <br/>
 * Reason: 阿里数据源配置类. <br/>
 * date: 2017年11月7日 下午7:16:07 <br/>
 *
 * @author atc
 * @version 1.0.0
 * @since JDK 1.8
 */
@Configuration
@PropertySource("classpath:mapper/druid.properties")
public class DataSourceConfig {

	private static final Logger log = LoggerFactory.getLogger(DataSourceConfig.class);

	/**
	 * env:spring通过这个bean可以获取配置文件中的值.
	 * @since JDK 1.8
	 */
	@Autowired
	private Environment env;
	
	@Bean
	public Properties test() {
		System.out.println("****");
		System.out.println(env.getProperty("druid.url"));
		System.out.println("****");
		return null;
	}
	
	@Bean(name = { "druid-prop" })
	public Properties getConfProp() {
		Resource location = new ClassPathResource("mapper/druid.properties");
		try {
			Properties properties = new Properties();
			properties.load(location.getInputStream());
			log.info("******加载数据库配置文件druid.properties******");
			log.debug("配置文件内容：" + properties);
			return properties;
		} catch (IOException e) {
			log.error("获取文件异常，读取数据库配置文件位置异常");
			e.printStackTrace();
		}
		return null;
	}

	@Bean(name = {"druidDataSource"})
	@Autowired
	public DruidDataSource getDtataSource(@Qualifier("druid-prop") Properties propertyResolver) {
		DruidDataSource druidDataSource = new DruidDataSource();
		druidDataSource.setDriverClassName(propertyResolver.getProperty("druid.driver-class-name"));
		druidDataSource.setUrl(propertyResolver.getProperty("druid.url"));
		druidDataSource.setUsername(propertyResolver.getProperty("druid.username"));
		druidDataSource.setPassword(propertyResolver.getProperty("druid.password"));
		druidDataSource.setInitialSize(Integer.parseInt(propertyResolver.getProperty("druid.initialSize")));
		druidDataSource.setMinIdle(Integer.parseInt(propertyResolver.getProperty("druid.minIdle")));
		druidDataSource.setMaxActive(Integer.parseInt(propertyResolver.getProperty("druid.maxActive")));
		druidDataSource.setMaxWait(Integer.parseInt(propertyResolver.getProperty("druid.maxWait")));
		druidDataSource.setTimeBetweenEvictionRunsMillis(
				Long.parseLong(propertyResolver.getProperty("druid.timeBetweenEvictionRunsMillis")));
		druidDataSource.setMinEvictableIdleTimeMillis(
				Long.parseLong(propertyResolver.getProperty("druid.minEvictableIdleTimeMillis")));
		druidDataSource.setValidationQuery(propertyResolver.getProperty("druid.validationQuery"));
		druidDataSource.setTestWhileIdle(Boolean.parseBoolean(propertyResolver.getProperty("druid.testWhileIdle")));
		druidDataSource.setTestOnBorrow(Boolean.parseBoolean(propertyResolver.getProperty("druid.testOnBorrow")));
		druidDataSource.setTestOnReturn(Boolean.parseBoolean(propertyResolver.getProperty("druid.testOnReturn")));
		druidDataSource.setPoolPreparedStatements(
				Boolean.parseBoolean(propertyResolver.getProperty("druid.poolPreparedStatements")));
		druidDataSource.setMaxPoolPreparedStatementPerConnectionSize(
				Integer.parseInt(propertyResolver.getProperty("druid.maxPoolPreparedStatementPerConnectionSize")));
		log.debug("数据源配置完成");
		return druidDataSource;
	}
}
